package net.kleinhenz.droidshop;

import java.util.List;

import net.kleinhenz.droidshop.R;
import net.kleinhenz.droidshop.model.Product;
import net.kleinhenz.droidshop.model.Review;
import android.app.ListActivity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class ReviewActivity extends ListActivity implements ListAdapter
{
	public static int MENUITEM_ADDWISHLIST = 0;
	public static int MENUITEM_WISHLIST = 1;
	public static int MENUITEM_HISTORY = 2;
	public static int MENUITEM_ABOUT = 3;
	public static int MENUITEM_SHARE = 4;

	private Product product = null;
	private List<Review> reviews = null; 

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		this.product = ((Product)getIntent().getExtras().get("product"));
		this.reviews = this.product.getReviews();
		getListView().setPadding(5, 5, 5, 5);
		getListView().setSmoothScrollbarEnabled(false);
		setListAdapter(this);
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		return getParent().onMenuItemSelected(featureId, item);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		LinearLayout layout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.reviewentry, null, false);
		
		TextView titleView = (TextView)layout.findViewById(R.id.Title);
		TextView textView = (TextView)layout.findViewById(R.id.Text);

		Review thisReview = reviews.get(position);
		titleView.setText(thisReview.getTitle());
		textView.setText(thisReview.getText());

		LinearLayout ratingLayout = (LinearLayout)layout.findViewById(R.id.RatingLayout);
		ProductActivity.createStars(ratingLayout, thisReview.getStars());
		
		return layout;
	}

	@Override
	public int getItemViewType(int position) 
	{
		return 0;
	}

	@Override
	public int getViewTypeCount() 
	{
		return 1;
	}

	@Override
	public Object getItem(int position) 
	{
		return reviews.get(position);
	}

	@Override
	public boolean areAllItemsEnabled() 
	{
		return false;
	}

	@Override
	public boolean isEnabled(int position) 
	{
		return false;
	}

	@Override
	public int getCount() 
	{
		return reviews.size();
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}

	@Override
	public boolean hasStableIds() 
	{
		return true;
	}

	@Override
	public boolean isEmpty() 
	{
		return reviews.isEmpty();
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) 
	{
		// NOP - Set never changes
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) 
	{
		// NOP - Set never changes
	}	
}
